package cn.com.shopec.erp.system.controller;

import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;

import cn.com.shopec.erp.core.base.controller.BaseController;
import cn.com.shopec.erp.system.constant.SystemConstant;
import cn.com.shopec.erp.system.service.IAccessService;
import cn.com.shopec.erp.system.vo.AccessVo;

/**
 * 
 * @ClassName AccessController
 * @Description 权限Controller
 * @author chengui
 * @date 2013-5-30 上午10:58:18
 */
@Controller
@RequestMapping("/manager/system/access")
public class AccessController extends BaseController {
	
	/** 日志. */
	private Logger log = Logger.getLogger(AccessController.class);
	
	@Resource private IAccessService accessService;
	
	/**
	 * 
	 * @MethodName accessList
	 * @Description TODO
	 * @author chengui
	 * @date 2013-5-31 上午10:39:19
	 * @return ModelAndView
	 * @throws Exception
	 */
	@RequestMapping("accessList")
	public ModelAndView accessList() throws Exception {
		return new ModelAndView("system/access/access_list");
	}
	
	/**
	 * 
	 * @MethodName addOrModifyMenu
	 * @Description 新增或修改菜单
	 * @author chengui
	 * @date 2013-5-31 下午2:21:06
	 * @return String
	 * @throws Exception
	 */
	@RequestMapping("addOrModifyMenu")
	@ResponseBody
	public String addOrModifyMenu(AccessVo accessVo) throws Exception {
		try {
			accessService.addOrModifyMenu(accessVo);
		} catch (Exception e) {
			log.info("addOrModifyMenu:" + e.getMessage());
			return SystemConstant.FAIL;
		}
		return SystemConstant.SUCCESS;
	}
	
	/**
	 * 
	 * @MethodName createAccessTreeTop
	 * @Description 异步加载菜单树
	 * @author chengui
	 * @date 2013-5-31 下午3:45:26
	 * @return String
	 * @throws Exception
	 */
	@RequestMapping("ajaxLoadAccessTree")
	@ResponseBody
	public String ajaxLoadAccessTree(AccessVo accessVo) throws Exception {
		List<AccessVo> accessVos = accessService.getMenuVos(accessVo, false, SystemConstant.MENU_TYPE);
		return JSONObject.toJSONString(accessVos);
	}
	
	/**
	 * 
	 * @MethodName deleteMenuNode
	 * @Description 删除菜单
	 * @author chengui
	 * @date 2013-6-1 下午12:40:10
	 * @param accessVo
	 * @return String
	 * @throws Exception
	 */
	@RequestMapping("deleteMenuNode")
	@ResponseBody
	public String deleteMenuNode(AccessVo accessVo) throws Exception {
		accessService.deleteMenuNode(accessVo);
		return SystemConstant.SUCCESS;
	}
	
}
